package com.yanqu.road.server.cmd.activity.fivehero;

import com.yanqu.road.entity.activity.fivehero.FiveHeroConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.FiveHeroProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;

import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_FIVEHERO_GET_RANK_REWARD, desc = "")
public class FiveHeroGetRankRewardCmd extends AbstractCommand {

    private FiveHeroProto.FiveHeroGetRankRewardReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = FiveHeroProto.FiveHeroGetRankRewardReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        FiveHeroConfig config = FiveHeroMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(config.getActivityInfo())) {
            ret = GameErrorCode.E_FIVE_HERO_NO_OPERATE_TIME;
            return;
        }

//        player.sendChannelPacket(CrossProtocol.C2_CROSS_FIVE_HERO_GET_RANK_REWARD, reqMsg.toBuilder());
        int groupId = GameUnionActivityGroupMgr.getGroupId(reqMsg.getActivityId(),player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_FIVE_HERO_GET_RANK_REWARD,
                reqMsg.toBuilder(),player.getUserId(),reqMsg.getActivityId(),groupId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }

        FiveHeroProto.FiveHeroGetRankRewardRespMsg.Builder respMsg = FiveHeroProto.FiveHeroGetRankRewardRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(ClientProtocol.U_FIVEHERO_GET_RANK_REWARD, respMsg);
    }
}
